<template>
  <div class="app-container">
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="基准利率代码" prop="baseRateSet" label-width="80">
        <el-input v-model="queryParams.baseRateSet" placeholder="请输入基准利率代码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="币种" prop="currency">
        <el-input v-model="queryParams.currency" placeholder="请输入币种" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="基准利率值" prop="baseRate">
        <el-input v-model="queryParams.baseRate" placeholder="请输入基准利率值" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="月数从" prop="monthsFrom">
        <el-input v-model="queryParams.monthsFrom" placeholder="请输入月数从" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="月数到(含)" prop="monthsTo">
        <el-input v-model="queryParams.monthsTo" placeholder="请输入月数到(含)" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="queryParams.description" placeholder="请输入描述" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>-->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd" v-hasPermi="['system:baseRate:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['system:baseRate:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['system:baseRate:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['system:baseRate:export']">导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseRateList" @selection-change="handleSelectionChange" height="calc(100vh - 320px)">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="币种" align="center" prop="currency"/>
      <el-table-column label="基准利率值" align="center" prop="baseRate"/>
<!--      <el-table-column label="月数从" align="center" prop="monthsFrom"/>
      <el-table-column label="月数到(含)" align="center" prop="monthsTo"/>-->
      <el-table-column label="描述" align="center" prop="description"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['system:baseRate:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)" v-hasPermi="['system:baseRate:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改基准利率对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
<!--        <el-form-item label="基准利率代码" prop="baseRateSet">
          <el-input v-model="form.baseRateSet" placeholder="请输入基准利率代码"/>
        </el-form-item>-->
        <el-form-item label="币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入币种" style="width: 85%"/>
        </el-form-item>
        <el-form-item label="基准利率值" prop="baseRate">
          <el-input v-model="form.baseRate" placeholder="请输入基准利率值" style="width: 85%"/>
        </el-form-item>
<!--        <el-form-item label="月数从" prop="monthsFrom">
          <el-input v-model="form.monthsFrom" placeholder="请输入月数从"/>
        </el-form-item>
        <el-form-item label="月数到(含)" prop="monthsTo">
          <el-input v-model="form.monthsTo" placeholder="请输入月数到(含)"/>
        </el-form-item>-->
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" style="width: 85%"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listBaseRate, getBaseRate, delBaseRate, addBaseRate, updateBaseRate} from "@/api/basic/baseRate";

export default {
  name: "BaseRate",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基准利率表格数据
      baseRateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        baseRateSet: null,
        currency: null,
        baseRate: null,
        monthsFrom: null,
        monthsTo: null,
        description: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    const baseRateSet = this.$route.query&&this.$route.query.baseRateSet;
    this.queryParams.baseRateSet = baseRateSet;
    this.getList();
  },
  methods: {
    /** 查询基准利率列表 */
    getList() {
      this.loading = true;
      listBaseRate(this.queryParams).then(response => {
        this.baseRateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        baseRateId: null,
        //baseRateSet: null,
        currency: null,
        baseRate: null,
        monthsFrom: null,
        monthsTo: null,
        description: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.baseRateId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加基准利率";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const baseRateId = row.baseRateId || this.ids
      getBaseRate(baseRateId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基准利率";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.baseRateSet = this.queryParams.baseRateSet;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.baseRateId != null) {
            updateBaseRate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBaseRate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const baseRateIds = row.baseRateId || this.ids;
      this.$modal.confirm('是否确认删除基准利率编号为"' + baseRateIds + '"的数据项？').then(function () {
        return delBaseRate(baseRateIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/baseRate/export', {
        ...this.queryParams
      }, `baseRate_${new Date().getTime()}.xlsx`)
    },
    /** 返回按钮操作 */
    handleClose() {
      const obj = { path: "/basicdata/baseRateSet" };
      this.$tab.closeOpenPage(obj);
    },
  }
};
</script>
